/* 
 * File:   TranscriberMetaInfo.cpp
 * Author: Guilherme Monteiro
 * 
 */

#include "../headers/TranscriberMetaInfo.h"

/**
 * Construtor1 of class.
 * 
 * @param imagePath: image path received of entry
 * @param model: generic model
 * 
 */
TranscriberMetaInfo::TranscriberMetaInfo(string imagePath, GenericModel* model) 
        throw (InvalidModelException, NullImageException) : MetaInfo() {
       
    try { 
        setOriginalPath(imagePath);
    } 
    catch (NullImageException &nie) {
        throw nie;
    }
    
    try {
        setModelName(model);
    }
    catch(InvalidModelException &ime) {
        throw ime;
    }
    
    setMetadata();
}

TranscriberMetaInfo::TranscriberMetaInfo(const TranscriberMetaInfo& orig) : MetaInfo(orig) {
}

TranscriberMetaInfo::~TranscriberMetaInfo() {
}

/**
 * Sets the path to high resolution image.
 * 
 * @param imagePath
 */
void TranscriberMetaInfo::setOriginalPath(string imagePath) throw (NullImageException) {
    if(!(pathValidator->validatePathToPages(pathValidator->LOW_PAGE, imagePath))) {
        throw NullImageException(imagePath);
    }
    else {
        this->originalPath = imagePath;
    }
}

/**
 * Configures the high resolution image path.
 * 
 * @param newHighPath
 */
void TranscriberMetaInfo::setHighResolutionImagePath(string newHighPath) throw (NullImageException) {
    if(!pathValidator->validatePathToPages(pathValidator->HIGH_PAGE, newHighPath)) {
        throw NullImageException(newHighPath);
    }
    else {
        this->highResolutionImagePath = newHighPath;
    }
    //printf("high path: %s\n", highResolutionImagePath.c_str());
}

/**
 * Configures the low resolution image path.
 * 
 * @param newLowPath
 */
void TranscriberMetaInfo::setLowResolutionImagePath(string newLowPath) throw (NullImageException) {
    if(!pathValidator->validatePathToPages(pathValidator->LOW_PAGE, newLowPath)) {
        throw NullImageException(newLowPath);
    }
    else {
        this->lowResolutionImagePath = newLowPath;
    }
    //printf("low path: %s\n", lowResolutionImagePath.c_str());
}

/**
 * Configures the entry file path.
 */
void TranscriberMetaInfo::setInputFilePath() {
    this->inputFilePath = ".." + BOOKS + getBookName() + METADADOS + ENTRADA + 
            getPageName().replace(getPageName().size()-4, getPageName().size(), ".txt");
    //printf("input file path: %s\n", this->getInputFilePath().c_str());
}

/**
 * Returns the entry file path.
 * 
 * @return entry file path
 */
string TranscriberMetaInfo::getInputFilePath() {
    return this->inputFilePath;
}


/**
 * Configures the output file path.
 */
void TranscriberMetaInfo::setOutputFilePath() {
    this->outputFilePath = ".." + BOOKS + getBookName() + METADADOS + SAIDA + 
            getPageName().replace(getPageName().size()-4, getPageName().size(), "") + "_" +
            getModelName() + ".txt";
    //printf("output file path: %s\n", this->getOutputFilePath().c_str());
}

/**
 * Returns the output file path.
 * 
 * @return output file path
 */
string TranscriberMetaInfo::getOutputFilePath() {
    return this->outputFilePath;
}

/**
 * Configures the incomplete log image
 * path. The path is completed in ROI class
 * that have the indexROI.
 */
void TranscriberMetaInfo::setLogImageIncompletePath() {
    this->logImageIncompletePath = ".." + LOG_IMAGES +
            getPageName().replace(getPageName().size()-4, getPageName().size(), "") + "_" +
            getModelName();
//    string page = getPageName().replace(getPageName().size()-4, getPageName().size(), "");
//    string exp = "../experience";
//    this->logImageIncompletePath = exp + getBookName() + page + page;
}

/**
 * Returns the incomplete path to log image.
 * 
 * @return path to log image 
 */
string TranscriberMetaInfo::getLogImageIncompletePath() {
    return this->logImageIncompletePath;
}

/**
 * Return the model preview path.
 * 
 * @return model preview path
 */
string TranscriberMetaInfo::getModelPreviewIncompletePath() {
    return this->modelPreviewPath;
}

/**
 * Sets the model preview path.
 */
void TranscriberMetaInfo::setModelPreviewIncompletePath() {
    this->modelPreviewPath = ".." + BOOKS + getBookName() +  METADADOS + MODEL_PREVIEW + 
            getPageName().replace(getPageName().size()-4, getPageName().size(), "") + "_" +
            getModelName();
//    printf("modelPreviewPath: %s\n", this->modelPreviewPath.c_str());
}

/**
 * Configures the metadata.
 * 
 */
void TranscriberMetaInfo::setMetadata() {
    setSubMetadata();
    
    MetaInfo::setHighResolutionImagePath();
    MetaInfo::setLowResolutionImagePath();
    setOutputFilePath();
    setInputFilePath();
    setModelPreviewIncompletePath();
    
    setLowResolutionImageCutIncompletePath();
    setHighResolutionImageCutIncompletePath();
    
    setLogImageIncompletePath();
}

/**
 * Configures the submetadata that
 * are needs to forms the used metadata.
 * The submetada are: bookName and pageName
 * 
 */
void TranscriberMetaInfo::setSubMetadata() {
    int i = 1, j = 0;
    string aux = "";
    while(j < originalPath.length()-1) {
        if(originalPath.at(j) == '/') {
            if(i == 3) {
                setBookName(aux);
//                printf("book name: %s\n", this->bookName.c_str());
            }
            else if(i == 4) {
                setPageName(originalPath.substr(j, originalPath.length()-j));
//                printf("page name: %s\n", this->pageName.c_str());
            }
            aux = "";
            i++;
        }
        aux += originalPath.at(j++);
    }
}

/**
 * Returns the model name.
 * The model name is used in output file path.
 * 
 * @return model name
 */
string TranscriberMetaInfo::getModelName() {
    return this->modelName;
}

/**
 * Sets the model name.
 * 
 * @param model
 */
void TranscriberMetaInfo::setModelName(GenericModel* model) throw (InvalidModelException) {
    if(&model != NULL && !model->getModelName().empty()) {
        this->modelName = model->getModelName();
    }
    else {
        throw InvalidModelException();
    }
}

/**
 * Configures the incomplete path
 * to image cut, this path will be without
 * the index roi. The path is completed in ROI class
 * that have the indexROI.
 * 
 */
void TranscriberMetaInfo::setLowResolutionImageCutIncompletePath() {
    this->lowResolutionImageCutIncompletePath = ".." + BOOKS + getBookName() + METADADOS + TABELAS_BAIXA + 
            getPageName().replace(getPageName().size()-4, getPageName().size(), "");
}

/**
 * Configures the incomplete path
 * to image cut, this path will be without
 * the index roi. The path is completed in ROI class
 * that have the indexROI.
 * 
 */
void TranscriberMetaInfo::setHighResolutionImageCutIncompletePath() {
    this->highResolutionImageCutIncompletePath = ".." + BOOKS + getBookName() + METADADOS + TABELAS_ALTA + 
            getPageName().replace(getPageName().size()-4, getPageName().size(), "");
}

/**
 * Returns the low resolution image
 * cut path.
 * 
 * obs.: note that the path is completed here
 * with the index roi parameter.
 * 
 * @return lowResolutionImageCutPath
 */
string TranscriberMetaInfo::getLowResolutionImageCutIncompletePath() {
    return this->lowResolutionImageCutIncompletePath;
}


/**
 * Returns the high resolution image
 * cut path.
 * 
 * obs.: note that the path is completed here
 * with the index roi parameter.
 * 
 * @return highResolutionImageCutPath
 */
string TranscriberMetaInfo::getHighResolutionImageCutIncompletePath() {
    return this->highResolutionImageCutIncompletePath;
}
